﻿using RBAC.LuFang.Domain;
using RBAC.LuFang.Domain.DTO;
using RBAC.LuFang.Repository.lmpl;
using RBAC.LuFang.Repository.lnterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.LuFang.Domin.Services
{
    public class RoleMenuService: IRoleMenuService
    {
        private readonly IRoleMenuRepository _roleMenuRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IMenuRepository _menuRepository;
        private readonly IUserRepository _userRepository;
        private readonly IUserRoleRepository _userRoleRepository;

        public RoleMenuService(IRoleMenuRepository roleMenuRepository, IRoleRepository roleRepository, IMenuRepository menuRepository,IUserRoleRepository userRoleRepository,IUserRepository userRepository)
        {
            _roleMenuRepository = roleMenuRepository;
            _roleRepository = roleRepository;
            _menuRepository = menuRepository;
            _userRepository = userRepository;
            _userRoleRepository = userRoleRepository;
        }
        /// <summary>
        /// 级联
        /// </summary>
        public class Menutun1
        {
            public int value { get; set; }
            public string? label { get; set; }
            public string? address { get; set; }
            public List<Menutun1> children { get; set; }
        }
        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menutun1>> GetRole()
        {
            try
            {
                var res = (await _roleRepository.GetAllAsync()).ToList();
                var list= res.Select(x => new Menutun1
                {
                    value = x.RoleId,
                    label = x.RoleName
                }).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }


    


        /// <summary>
        /// 显示权限菜单 使用递归查询子集
        /// </summary>
        /// <returns></returns>
        public async Task<List<Menutun1>> GetMenu()
        {
            try
            {
                var res = (await _menuRepository.GetAllAsync()).ToList();
                var res1 = (await _menuRepository.GetAllAsync()).Where(x => x.Pid == 0);
                List<Menutun1> respone = new List<Menutun1>();
                foreach (var menu in res1)
                {
                    Menutun1 menus1 = new Menutun1();
                    menus1.value = menu.menuInfoId;
                    menus1.label = menu.menuInfoName;
                    menus1.address = menu.menuInfoUrl;
                    menus1.children = new List<Menutun1>();
                    GetPidvalue(res, menus1);
                    respone.Add(menus1);
                }
                return respone;
            }
            catch (Exception)
            {

                throw;
            }
        }
        //调用递归
        private void GetPidvalue(List<MenuInfo> menu, Menutun1 menutun)
        {
            var list = _menuRepository.GetAll().Where(x => x.Pid == menutun.value);
            foreach (var item in list)
            {
                Menutun1 model = new Menutun1();
                model.value = item.menuInfoId;
                model.label = item.menuInfoName;
                model.address = item.menuInfoUrl;
                model.children=new List<Menutun1>();
                menutun.children.Add(model);
                GetPidvalue(menu,model);
            }
        }
        /// <summary>
        /// 根据角色id反填菜单
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<List<FanMenuDTO>> FanMenu(int rid)
        {
            try
            {
                var res =await _roleMenuRepository.Get(x=>x.RoleId==rid);
                var list = res.ToList().Select(x => new FanMenuDTO
                {
                    menuInfoId=x.menuInfoId
                }).ToList();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 角色菜单添加
        /// </summary>
        /// <param name="rid"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public async Task<int> AddRoleMenu(int rid, List<int> mid)
        {
            try
            {
                var res1 = 0;
                foreach (var item in mid)
                {
                    var rea = await _roleMenuRepository.Get(x=>x.RoleId==rid && x.menuInfoId==item);
                    if (rea.Count() > 0)
                    {
                        res1 = -1;
                    }
                    else
                    {
                        res1 = 1;
                        var res = new RoleMenu();
                        res.RoleId = rid;
                        res.menuInfoId = item;
                        await _roleMenuRepository.GetAddAsync(res);
                    }
                }
                return res1;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public class GetMenu1DTO
        {
            public string userName { get; set; }
            public string menuInfoUrl { get; set; }
            public string menuInfoName { get; set; }
        }
        /// <summary>
        /// 显示菜单（没有用递归）
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task<List<GetMenu1DTO>> GetMenu1(int uid)
        {
            try
            {
                var res = from u in await _userRepository.GetAllAsync()
                          join ur in await _userRoleRepository.GetAllAsync() on u.userInfoID equals ur.userInfoID
                          join rm in await _roleMenuRepository.GetAllAsync() on ur.RoleId equals rm.RoleId
                          join menu in await _menuRepository.GetAllAsync() on rm.menuInfoId equals menu.menuInfoId
                          where u.userInfoID == uid && ur.IsDel==false
                          select new 
                          {
                             u.userName,
                             menu.menuInfoUrl,
                             menu.menuInfoName,
                          };
                var dto = res.ToList().Select(x => new GetMenu1DTO
                {
                    userName=x.userName,
                    menuInfoUrl=x.menuInfoUrl,
                    menuInfoName=x.menuInfoName,
                }).ToList();
                return dto;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
